package popcorn.ticket.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.MovieHallsMapper;
import popcorn.ticket.entity.MovieHalls;
import popcorn.ticket.entity.MovieHallsExample;
import popcorn.ticket.service.MovieHallsService;
import popcorn.ticket.tool.PageData;

/**
 * 影厅服务实现层
 * @author lenovo
 *
 */
@Service
public class MovieHallsServiceImpl implements MovieHallsService{
@Autowired 
MovieHallsMapper  mhm;	
/**
 * 添加影厅
 */
	@Override
	public int addMovieHalls(MovieHalls mh) {
		return mhm.insert(mh);
	}
/**
 * 根据id删除影厅
 */
	@Override
	public int delMovieHalls(int hallid) {
		return mhm.deleteByPrimaryKey(hallid);
	}
/**
 * 修改影厅信息
 */
	@Override
	public int updMovieHalls(MovieHalls mh) {
		// TODO Auto-generated method stub
		return mhm.updateByPrimaryKey(mh);
	}

	@Override
	public PageData<MovieHalls> selectMovieHallsByPage(int page, int limit, String keywords) {
		PageHelper.startPage(page, limit);
		// 模糊查询
		MovieHallsExample mhe=new MovieHallsExample();
		//根据影厅名称模糊查询
		mhe.createCriteria().andHallnameLike("%"+keywords+"%");
		List<MovieHalls> list = mhm.selectByExample(mhe);
		PageInfo<MovieHalls>  pageInfo= new PageInfo<MovieHalls>(list);

		//数据
		List<MovieHalls>  data=pageInfo.getList();
 		//总记录数
		int count=(int) pageInfo.getTotal();
		//前端要求
		PageData<MovieHalls>  pd=new PageData<MovieHalls>(data, page, count, limit);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}
	
	/**
	 * 批量删除
	 */
	@Override
	public int delAllMovieHall(String array) {
		String[] idStr = array.split(",");
		//将 字符串类型的数组 转换成INT类型的list
		ArrayList<Integer> list=new ArrayList<Integer>();
		for (int i = 0; i < idStr.length; i++) {
			// 强制转型
			 list.add(Integer.parseInt(idStr[i]));
		}	
		MovieHallsExample be=new MovieHallsExample();
		be.createCriteria().andHallidIn(list);
		int n=mhm.deleteByExample(be);

		return n;
	}


}
